package com.zhenyi.jiahua.index.admin;

import java.util.Date;

import org.joda.time.DateTime;

import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jiahua.common.model.JhFollow;
import com.jiahua.common.model.JhStudent;
import com.utils.DateUtils;
import com.zhenyi.jiahua.index.JHController;

import cn.hutool.core.date.DateUtil;

/**
 * 学生跟进
 * 
 * @author cen
 *
 */
public class StudentFollowController extends JHController {

	@Override
	public void index() {
		Long studentId = getParaToLong("studentId");
		String startDate = getPara("startDate",
				DateUtil.format(new DateTime().minusDays(1).toDate(), DateUtils.YYYYMMDD_HHMMSS));
		String endDate = getPara("endDate", DateUtil.format(new DateTime().toDate(), DateUtils.YYYYMMDD_HHMMSS));
		Long staffId = getParaToLong("staffId");

		if (studentId == null) {
			renderFailure(500, "student id is null");
			return;
		}
		JhStudent student = JhStudent.dao.findById(studentId);

		Kv kv = Kv.create();
		kv.set("studentId", studentId);
		if (StrKit.notBlank(startDate) && StrKit.notBlank(endDate)) {
			kv.set("startTime", startDate);
			kv.set("endTime", endDate);
		}
		if (staffId != null) {
			kv.set("staffId", staffId);
		}
		SqlPara sqlPara = Db.use("jiahua").getSqlPara("JHStudentFollow", kv);
		Page<Record> page = Db.use("jiahua").paginate(getPage(), PAGE_SIZE, sqlPara);
		setRainbow(page);
		setItems(jsonObject(page.getList()));
		setInfoItem(student);
		setAttr("startDate", startDate);
		setAttr("endDate", endDate);
		render("/jh/admin/student/follow.html");
	}

	public void info() {
		Long followId = getParaToLong("followId");
		Long studentId = getParaToLong("studentId");
		if (followId != null) {
			JhFollow info = JhFollow.dao.findById(followId);
			if (info == null) {
				renderFailure(404, "record not found");
				return;
			}
			JhStudent student = JhStudent.dao.findById(info.getStudentId());
			setAttr("student", student);
			setInfoItem(info);
		} else if (studentId != null) {
			setAttr("student", JhStudent.dao.findById(studentId));
		} else {
			renderFailure();
			return;
		}
		render("/jh/admin/student/followInfo.html");
	}

	public void submit() {
		Long followId = getParaToLong("followId");
		String content = getParaRelaceN("content");
		String nextDate = getPara("nextDate");
		Long studentId = getParaToLong("studentId");
		if (studentId == null) {
			renderFailure(500, "student id is null");
			return;
		}
		JhStudent student = JhStudent.dao.findById(studentId);
		if (student == null) {
			renderError(404);
			return;
		}
		student.setIsFollow(1);
		student.update();

		String studentName = student.getName();
		String mobile = student.getMobile();
		boolean isNew = followId == null;
		JhFollow info = isNew ? new JhFollow() : JhFollow.dao.findById(followId);
		if (!isNew && info == null) {
			renderFailure(404, "record not found");
			return;
		}
		info.setDate(new Date());
		info.setContent("[" + studentName + " " + mobile + "]" + content);
		if (StrKit.notBlank(nextDate)) {
			info.setNextDate(DateUtil.parse(nextDate, DateUtils.YYYYMMDD_HHMMSS));
			info.setNotiType(JhFollow.TYPE_NOTI);
		}
		if (isNew) {
			info.setStudentId(studentId);
			info.setStaffId(getStaff().getId());
			info.setAddTime();
		}
		boolean flag = isNew ? info.save() : info.update();
		if (flag) {
			renderSuccess();
		} else {
			renderFailure();
		}
	}

	public void delete() {
		Long followId = getParaToLong("followId");
		if (followId == null) {
			renderFailure(500, "id is null");
			return;
		}
		JhFollow info = JhFollow.dao.findById(followId);
		if (info == null) {
			renderFailure(404, "record not found");
			return;
		}
		renderFlag(info.delete());
	}

}
